package com.crazy.twist.web;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.extension.BizScenario;
import com.crazy.twist.api.OrderServiceI;
import com.crazy.twist.dto.cmd.OrderCreateCmd;
import com.crazy.twist.dto.data.OrderDTO;
import com.crazy.twist.dto.qry.OrderPreviewQry;
import com.crazy.twist.model.FeatureModel;
import com.crazy.twist.model.OrderCreateRequest;
import com.crazy.twist.model.OrderPreviewRequest;
import com.crazy.twist.order.constant.Constants;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName OrderController
 * @Author mengke
 * @create 2022/5/7 00:10
 * @Description
 */
@RestController
@RequestMapping("tob/order")
public class OrderController {

    @Autowired
    private OrderServiceI orderServiceI;

    @PostMapping("/fork/preview")
    public SingleResponse<OrderDTO> previewOrder(@RequestBody OrderPreviewRequest orderPreviewRequest) {
        OrderPreviewQry orderPreviewQry = OrderPreviewQry.builder()
                .bizScenario(BizScenario.valueOf(Constants.BIZ_TOB, Constants.USE_CASE_ZD_PORK, Constants.ORDER_PREVIEW))
                .biz(Constants.BIZ_TOB)
                .build();
        return orderServiceI.previewOrder(orderPreviewQry);
    }

    @PostMapping("/fork/create")
    public Response createOrder(@RequestBody OrderCreateRequest orderCreateRequest) {
        // 接口校验
        // 数据转换 为 cmd 模式
        OrderCreateCmd orderCreateCmd = OrderCreateCmd.builder()
                .orderDTO(new OrderDTO())
                .biz(Constants.BIZ_TOB)
                .bizScenario(BizScenario.valueOf(Constants.BIZ_TOB, Constants.USE_CASE_ZD_PORK, Constants.ORDER_CREATE))
                .build();

        List list = Lists.newArrayList();

        return orderServiceI.createOrder(orderCreateCmd);
    }


    @PostMapping("/getFeatures")
    public List<FeatureModel> getFeatures(@RequestBody List<FeatureModel> featureModelList) {

        List<FeatureModel> featureModels = Lists.newArrayList();

        // 初始复制 lift
        for (FeatureModel featureModel : featureModelList) {
            featureModel.setFpd7R(featureModel.getFpd7() / featureModel.getFpd7Finish());
            featureModel.setLift(featureModel.getFpd7R() / featureModel.getConstant());
        }

        // 定义初始参数
        Double init = featureModelList.get(0).getLift();
        // 给结果集赋初始值
        featureModels.add(featureModelList.get(0));

        // 赋值初始单调情况
        boolean increase = init < 1;

        int skip = 0;

        for (int i = 1; i < featureModelList.size(); i++) {
            if (increase) {
                if (featureModelList.get(i).getLift() - featureModelList.get(i - 1).getLift() < 0) {
                    FeatureModel mergeResult = merge(featureModelList.get(i), featureModelList.get(i - 1), increase);
                    featureModels.set(i - 1, mergeResult);
                    skip++;
                } else {
                    if (skip == 0) {
                        featureModels.add(featureModelList.get(i));
                    }
                    if (skip > 0) {
                        skip--;
                    }

                }
            }
        }
        return featureModels;
    }

    private FeatureModel merge(FeatureModel after, FeatureModel before, boolean increase) {
        FeatureModel featureModel = new FeatureModel();
        List<Double> score = Lists.newArrayList();
        score.addAll(after.getScore());
        score.addAll(before.getScore());
        Collections.sort(score);

        featureModel.setScore(Lists.newArrayList(score.get(0), score.get(3)));
        featureModel.setFpd7(after.getFpd7() + before.getFpd7());
        featureModel.setFpd7Finish(after.getFpd7Finish() + before.getFpd7Finish());
        featureModel.setFpd7R(featureModel.getFpd7() / featureModel.getFpd7Finish());
        featureModel.setConstant(after.getConstant());
        featureModel.setLift(featureModel.getFpd7R() / after.getConstant());
        return featureModel;
    }


}
